<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>管理员管理</title>
		<link rel="stylesheet" href="/static/css/layui.css" />
		<link rel="stylesheet" href="/static/css/common.css" />
	</head>
	<body class="container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">用户名</label>
							<div class="layui-input-inline">
								<input type="text" name="username" placeholder="" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">手机号</label>
							<div class="layui-input-inline">
								<input type="text" name="mobile" placeholder="" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<button class="layui-btn layui-btn-md layui-btn-primary" lay-submit lay-filter="user-query">
								<i class="layui-icon layui-icon-search"></i>
								查询
							</button>
							<button type="reset" class="layui-btn layui-btn-md" lay-filter="user-reset" lay-submit>
								<i class="layui-icon layui-icon-refresh"></i>
								重置
							</button>
						</div>
					</div>
				</form>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="user-table" lay-filter="user-table"></table>
			</div>
		</div>

		<script type="text/html" id="user-toolbar">
			<button class="layui-btn layui-btn-primary layui-btn-md" lay-event="add">
		        <i class="layui-icon layui-icon-add-1"></i>
		        新增
		    </button>
		    
		</script>

		<script type="text/html" id="user-bar">
			<button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
		    <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
		</script>

		<script type="text/html" id="user-enable">
			<input type="checkbox" name="status" value="{{d.admin_id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="user-enable" {{ d.status== 0 ? 'checked' : '' }} />
		</script>

		

		<script type="text/html" id="user-loginTime">
			{{layui.util.toDateString(d.login_time*1000, 'yyyy-MM-dd HH:mm:ss')}}
		</script>
		
		<script src="/static/js/layui.js"></script>
		<script>
			layui.extend({
					api: '{/}/static/js/api' // 开头特定符 {/} 即代表采用单独路径
				});
			layui.use(['api'], function() {
				let api = layui.api;
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let common = layui.common;

				let MODULE_PATH = "./";

				let cols = [
					[
						{
							title: 'ID',
							field: 'admin_id',
							align: 'center'
						},
						{
							title: '账号',
							field: 'username',
							align: 'center',
							width: 100
						},
						{
							title: '姓名',
							field: 'name',
							align: 'center'
						},
						{
							title: '电话',
							field: 'mobile',
							align: 'center'
						},
						{
							title: '启用',
							field: 'status',
							align: 'center',
							templet: '#user-enable'
						},
						{
							title: '登录次数',
							field: 'login_num',
							align: 'center'
						},
						{
							title: '登录时间',
							field: 'login_time',
							align: 'center',
							templet: '#user-loginTime'
						},
						{
							title: '操作',
							toolbar: '#user-bar',
							align: 'center',
							width: 130
						}
					]
				]

				table.render({
					elem: '#user-table',
					url: api.apiUrl + 'admin/list',
					headers : api.getHeader(),
					parseData: function(res) {
						//console.log(res)
						return {
							"code": res.code, //解析接口状态
							"msg": res.message, //解析提示文本
							"count": res.data.count, //解析数据长度
							"data": res.data.list //解析数据列表
						};
					},
					page: true,
					cols: cols,
					skin: 'line',

					toolbar: '#user-toolbar',
					defaultToolbar: [{
						title: '刷新',
						layEvent: 'refresh',
						icon: 'layui-icon-refresh',
					}, 'filter', 'print', 'exports']
				});

				table.on('tool(user-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					}
				});

				table.on('toolbar(user-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					} else if (obj.event === 'batchRemove') {
						window.batchRemove(obj);
					}
				});
				// 监听搜索操作

				form.on('submit(user-query)', function(data) {
					table.reload('user-table', {
						where: {
							param : $('form').serialize()
						},
						page: {
					        curr: 1
					    }
					})
					return false;
				});
				form.on('submit(user-reset)', function(data){
					table.reload('user-table', {
						where: {
							param : ''
						},
						page: {
					        curr: 1
					    }
					});
					return false;
				})
				form.on('switch(user-enable)', function(data) {
					//layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
					let v = 1;
					if (data.elem.checked) v = 0;
					let postData = {
						id : data.value,
						status : v
					};
					api.post('admin/enable', postData, res => {
						//console.log(res)
					},err => {
						$(data.elem).prop('checked', false);
						form.render('checkbox');
					})
				});

				window.add = function() {
					layer.open({
						type: 2,
						title: '新增',
						shade: 0.1,
						offset: 'rt',
						area: ['80%', '100%'],
						anim: 1,
						maxmin: true,
						content: MODULE_PATH + 'add.html'
					});
				}

				window.edit = function(obj) {
					layer.open({
						type: 2,
						title: '修改',
						shade: 0.1,
						offset: 'rt',
						area: ['80%', '100%'],
						anim: 1,
						maxmin: true,
						content: MODULE_PATH + 'edit.html?id=' + obj.data.admin_id
					});
				}
				window.remove = function(obj){
		            layer.confirm('确定要删除该用户', {icon: 3, title:'提示'}, function(index){
		                layer.close(index);
		                api.post('admin/del',{admin_id : obj.data.admin_id}, res => {
							layer.msg('删除成功',{icon:1,time:1000},() => {
								obj.del();
							});
						}, err => {
							//layer.msg('删除失败',{icon:2,time:1000});
						})
		            });
		        }
				

				window.refresh = function(param) {
					table.reload('user-table');
				}
			})
		</script>
	</body>
</html>
